package com.arcvideo.acsplayer.utils;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.net.URLEncoder;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.arcvideo.acsplayer.app.AcsPlayerApplication;

/**
 * 获取跟app相关的信息，如versionName, versionCode, metaData等;
 *
 * @author zhoujun
 *
 */
public class AppInfoUtils {

	/**
	 * 获取版本name
	 *
	 * @param context
	 * @return
	 */
	public static String getAppVersionName(Context context) {
		String versionName = "";
		try {
			PackageManager pm = context.getPackageManager();
			PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
			versionName = pi.versionName;
		} catch (Exception e) {
			return versionName;
		}
		return versionName;
	}

	/**
	 * 获取AndroidManifest里面的metaData
	 *
	 * @param context
	 * @param name
	 *            metaData中的key
	 * @return
	 */
	public static Object getAppMetaData(Context context, String name) {
		try {
			PackageManager pm = context.getPackageManager();
			ApplicationInfo applicationInfo = pm.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
			if (applicationInfo != null && applicationInfo.metaData != null) {
				return applicationInfo.metaData.get(name);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取应用的渠道号
	 *
	 * @return
	 */
	public static String getAppChannel() {
		// try {
		// ApplicationInfo info =
		// getPackageManager().getApplicationInfo(getPackageName(),
		// PackageManager.GET_META_DATA);
		// String channel = info.metaData.getString("UMENG_CHANNEL");
		// if (channel == null) {
		// channel = String.valueOf(info.metaData.getInt("UMENG_CHANNEL"));
		// }
		//
		// return channel;
		//
		// } catch (NameNotFoundException e) {
		// e.printStackTrace();
		// }
		Object obj = getAppMetaData(AcsPlayerApplication.getAppContext(), "UMENG_CHANNEL");
		return obj == null ? "" : String.valueOf(obj);
	}

	// public static HashMap<String, String> getAppInfo(Context context) {
	// HashMap<String, String> map = new HashMap<String, String>();
	// try {
	// PackageManager pm = context.getPackageManager();
	// PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
	// map.put("id", Build.MODEL);
	// map.put("rom", Build.VERSION.RELEASE);
	// map.put("appName", context.getPackageName());
	// map.put("versionName", pi.versionName);
	// map.put("versionCode", Integer.toString(pi.versionCode));
	// return map;
	// } catch (Exception e) {
	// return map;
	// }
	// }

	/**
	 * 获取应用的版本号
	 *
	 * @return
	 */
	public static int getAppVersionCode() {
		try {
			Context context = AcsPlayerApplication.getAppContext();
			PackageManager pm = context.getPackageManager();
			PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
			return pi.versionCode;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	public static String getMacAdress(Context context) {
		String macString = "";
		try {
			macString = PreferenceManager.getDefaultSharedPreferences(context).getString("mac", "");
			if (!TextUtils.isEmpty(macString)) {
				return macString;
			}
			WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
			WifiInfo info = (null == wifiMgr ? null : wifiMgr.getConnectionInfo());
			if (info != null) {
				macString = info.getMacAddress();
			}
			if (TextUtils.isEmpty(macString)) {
				macString = "";
			}
			PreferenceManager.getDefaultSharedPreferences(context).edit().putString("macAddr", macString).commit();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return macString;
	}

	/*
	 * 新的唯一标识算法，imei+android_id+serialno
	 */
	public static String getTokenM2() {
		String imei = getDeviceId(AcsPlayerApplication.getAppContext());
		String AndroidID = android.provider.Settings.System.getString(AcsPlayerApplication.getAppContext().getContentResolver(),
				"android_id");
		String serialNo = getDeviceSerial();
		return MD5Utils.encode(imei + AndroidID + serialNo);
	}

	public static String getDeviceId(Context context) {
		TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		return tm.getDeviceId();
	}

	private static String getDeviceSerial() {
		String serial = "";
		try {
			Class<?> c = Class.forName("android.os.SystemProperties");
			Method get = c.getMethod("get", String.class);
			serial = (String) get.invoke(c, "ro.serialno");
		} catch (Exception ignored) {
		}
		return serial;
	}

	private static String mUserAgent = null;

	public static String getUserAgent() {
		if (TextUtils.isEmpty(mUserAgent)) {
			String versionName = getAppVersionName(AcsPlayerApplication.getAppContext());
			mUserAgent = "360 Video App/" + versionName + " Android/OSVer QIHU";
			String ver = android.os.Build.VERSION.RELEASE;
			if (ver != null) {
				try {
					ver = URLEncoder.encode(ver, "UTF-8").replaceAll("\\+", "%20");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			}
			mUserAgent = mUserAgent.replace("OSVer", ver);
		}
		return mUserAgent;
	}
}